package com.hq.fxh.No2_BBS.Fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.hq.fxh.No2_BBS.Activity.Plate_DetialActivity;
import com.hq.fxh.No2_BBS.Adapter.RAdater_KanTieList;
import com.hq.fxh.Modle.PlateListModle;
import com.hq.fxh.R;
import com.hq.fxh.Utils.LogUtils;
import com.hq.fxh.HttpApi.UserServiceImpl;
import com.liaoinstan.springview.widget.SpringView;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * /**
 *
 * @author 薛
 * @ClassName
 * @Description : 板块详情帖子列表页
 * @Created 2017/8/20
 * @Email xlwenhao1858@sina.com
 */


public class PlateSortDetailFragment extends Fragment implements AppBarLayout.OnOffsetChangedListener {
    protected View rootView;
    protected RecyclerView recyclerView;
    protected SpringView circleFragmentTopicSpring;
    private RAdater_KanTieList adapter;
    int type, firstId, secondId;


    public void setType(int type, int firstId, int secondId) {
        this.type = type;
        this.secondId = secondId;
        this.firstId = firstId;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.test_myfragment, null);
        getData();
        initView(rootView);
        initData();
        return rootView;
    }

    private void getData() {
        UserServiceImpl.getPlateList(secondId, 1, 1, 20, new Callback<PlateListModle>() {

            @Override
            public void onResponse(Call<PlateListModle> call, Response<PlateListModle> response) {
                LogUtils.e("网络请求success" + response.body().toString());

                List<PlateListModle.DataBean> data = response.body().getData();
                if (data != null) {
                    adapter.setData(data);
                    adapter.notifyDataSetChanged();
                }

            }

            @Override
            public void onFailure(Call<PlateListModle> call, Throwable t) {
                LogUtils.e("网络请求" + t.getMessage());

            }
        });
    }

    private void initData() {
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        adapter = new RAdater_KanTieList(getContext(), new RAdater_KanTieList.KanTieCallBack() {
            @Override
            public void onCall(int position, LinearLayout layout) {

            }
        });
        recyclerView.setAdapter(adapter);
     /*   circleFragmentTopicSpring.setType(SpringView.Type.FOLLOW);
        circleFragmentTopicSpring.setGive(SpringView.Give.BOTH);
        circleFragmentTopicSpring.setFooter(new DefaultFooter(getActivity()));
        circleFragmentTopicSpring.setHeader(new DefaultHeader(getActivity()));
        circleFragmentTopicSpring.setListener(new SpringView.OnFreshListener() {
            @Override
            public void onRefresh() {
                LogUtils.e("刷新");
            }

            @Override
            public void onLoadmore() {
                LogUtils.e("加载");


            }
        });*/
    }


    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
    //    circleFragmentTopicSpring.setEnable(verticalOffset == 0);
    }

    private void initView(View rootView) {
        recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView);
     //   circleFragmentTopicSpring = (SpringView) rootView.findViewById(R.id.circle_fragment_topic_spring);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (Plate_DetialActivity.appBarLayout != null)
            Plate_DetialActivity.appBarLayout.addOnOffsetChangedListener(this);
    }
/*
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);

        *//**
         * 这段代码是为了解决springview 和tabBarLayout中嵌套时上下滚动冲突
         *//*
        if (isVisibleToUser && this.getContext() != null) {
          *//*  Plate_DetialActivity fragmentActivity = (Plate_DetialActivity) getActivity();*//*
            Space_UserActivity fragmentActivity = (Space_UserActivity) getActivity();

            if (fragmentActivity.appBarLayout != null) {
                fragmentActivity.appBarLayout.addOnOffsetChangedListener(this);
            } else if (isVisibleToUser && this.getContext() == null) {
                //viewpager中第一页加载的太早,getContext还拿不到,做个延迟
                new Handler().post(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            Thread.sleep(2000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        if (PlateSortDetailFragment.this.getContext() != null) {

                            // MyApplication application = (MyApplication) TopicParticularsFragment.this.getContext().getApplicationContext();

                            Plate_DetialActivity fragmentActivity = (Plate_DetialActivity) getActivity();
                            if (fragmentActivity.appBarLayout != null) {
                                fragmentActivity.appBarLayout.addOnOffsetChangedListener(PlateSortDetailFragment.this);
                            }
                        }
                    }
                });
            }
        }
    }*/

}
